package arch.service.impl.archive;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import arch.dao.archive.OperationDao;
import arch.domain.archive.Operation;
import arch.service.archive.OperationService;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
@Service("operationService")
public class OperationServiceBean implements OperationService {
	@Resource private OperationDao operationDao;
	
	public List<Operation> list() {
		String sql = "select id,key,name,rank,filter,action,description from ar_operation order by rank asc";
		
		return this.operationDao.list(sql);
	}
	@Transactional(propagation = Propagation.REQUIRED)
	public void add(Operation operation) {
		if(operation != null) {
			operation.setId(this.operationDao.maxInt("select max(id) from ar_operation") + 1);
			operation.setRank(this.operationDao.maxInt("select max(rank) from ar_operation") + 1);
			this.operationDao.add(operation);
		}
	}
	public Operation find(Integer id) {
		Operation result = null;
		if(id != null){
			result = this.operationDao.find(id);
		}
		return result;
	}
	public Operation findByKey(String key) {
		Operation operation = null;
		if(key != null && !"".equals(key.trim())) {
			operation = this.operationDao.findByKey(key);
		}
		return operation;
	}
	@Transactional(propagation = Propagation.REQUIRED)
	public void remove(Integer id) {
		if(id != null) {
			this.operationDao.remove(id);
		}
	}
	@Transactional(propagation = Propagation.REQUIRED)
	public void update(Operation operation) {
		if(operation != null && operation.getId() != null) {
			this.operationDao.merge(operation);
		}
	}
}
